package org.christopher.bank.controller;


import org.christopher.bank.entity.Department;
import org.christopher.bank.entity.Employee;
import org.christopher.bank.service.DepartmentService;
import org.christopher.bank.service.EmployeeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author christopher
 * @since 2021-09-28
 */
@Controller
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private DepartmentService departmentService;

    @GetMapping("/show_employee")
    public String shwoEmployee(Model model){
        List<Employee> employees = employeeService.selectEmployee();
        model.addAttribute("employees",employees);
        return "/employee/emp";
    }


    @PostMapping("/do_login")
    public String doLogin(Employee employee){
        Employee loginEmployee = employeeService.login(employee);
        if (null != loginEmployee){
            return "success";
        }
        return "error";
    }

    @GetMapping("/add_employee")
    public String addEmployee(Model model){
        List<Department> departments = departmentService.selectDepartAll();
        model.addAttribute("departs",departments);
        return "employee/add_employee";
    }

    @PostMapping("/do_add_employee")
    public String doAddEmployee(Employee employee){

        log.info("log{}",employee);
        employeeService.addEmployee(employee);

        return "redirect:/employee/show_employee";
    }

    @GetMapping("/show_emp_info/{id}")
    public String showEmployeeInfo(@PathVariable Integer id, Model model){
        Employee employee = employeeService.selectEmployeeById(id);
        model.addAttribute("emp",employee);
        return "employee/show_emp";
    }

    @GetMapping("/update_emp_info/{id}")
    public String updateEmployee(@PathVariable Integer id,Model model){
        Employee employee = employeeService.selectEmployeeById(id);
        List<Department> departments = departmentService.selectDepartAll();
        model.addAttribute("emp",employee);
        model.addAttribute("departs",departments);
        return "employee/update_emp";
    }

    @GetMapping("/delete_emp_info/{id}")
    public String deleteEmployee(@PathVariable Integer id){
        employeeService.deleteEmployee(id);

        return "redirect:/employee/show_employee";

    }
}

